
#include "adc_c.h"
						  
//extern void Producer(unsigned short data);
//*****************************************************************************
//
//! \addtogroup ADC
//!
//! ADC interrupt handler
//
//*****************************************************************************

void ADC0IntHandler(void)
{
	//
    // This array is used for storing the data read from the ADC FIFO. It
    // must be as large as the FIFO for the sequencer in use.  This example
    // uses sequence 3 which has a FIFO depth of 1.  If another sequence
    // was used with a deeper FIFO, then the array size must be changed.
    //
    unsigned long ulADC0_Value[1];
	//unsigned long ulTemp_ValueC;

	//char OLEDbuf[22];

    //
    // Clear the asserted interrupts.
    //
    ADCIntClear(ADC0_BASE, 3);

   	//
    // Read ADC Value.
    //
    ADCSequenceDataGet(ADC0_BASE, 3, ulADC0_Value);

	//Producer(ulADC0_Value[0]);

	//ulTemp_ValueC = ((1475 * 1023) - (2250 * ulADC0_Value[0])) / 10230;    

	//FifoPut ulADC0_Value 
	//IntMasterDisable();
	//sprintf(OLEDbuf,"Temperature = %3d*C",ulTemp_ValueC);
	//oLED_Message(0,0,OLEDbuf);
    //IntMasterEnable();
}





//*****************************************************************************
//
//! \addtogroup ADC
//!
//! Initializes the ADC0 for timer interrupt trigger, sequence 3, sampling
//!	from the temperature sensor
//! \param int channel from 0-7 and 0x80 for Temperature sensor
//
//*****************************************************************************

void InitADC0(int channel) {
	static int initialized = 0;
	if(initialized == 1)
		return;
	initialized = 1;

    // The ADC0 peripheral must be enabled for use.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    // Enable sample sequence 3 with a timer signal trigger.  Sequence 3
    // will do a single sample when the timer interrupts sends a singal to start the
    // conversion. 
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0);

    // Configure step 0 on sequence 3.  Sample the temperature sensor
    // (ADC_CTL_TS) and configure the interrupt flag (ADC_CTL_IE) to be set
    // when the sample is done.  .
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, (channel | ADC_CTL_IE |
                             ADC_CTL_END));

    // Since sample sequence 3 is now configured, it must be enabled.
    ADCSequenceEnable(ADC0_BASE, 3);

    // Clear the interrupt status flag.  This is done to make sure the
    // interrupt flag is cleared before we sample.
    ADCIntClear(ADC0_BASE, 3);

	//Enable the ADC0 interrupt for step sequence 3
	ADCIntEnable(ADC0_BASE, 3);
	IntEnable(INT_ADC0SS3);

}

//*****************************************************************************
//
//! \addtogroup ADC
//!
//! Initializes the ADC timer trigger at /period ms usint timer 0 32-bit periodic 
//! counter
//
//*****************************************************************************
void InitADCTimerTrigger(unsigned long period) {

//
    // The Timer0 peripheral must be enabled for use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

	
	//
    // Configure Timer0B as a 32-bit periodic timer.
    //
    TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR);

   	//
    // Set the Timer0 load value to 1ms.
    //
    TimerLoadSet(TIMER0_BASE, TIMER_A, (period * (SysCtlClockGet()/1000)));

	//
   	// Set up Timer to trigger ADC
   	//
   	TimerControlTrigger(TIMER0_BASE, TIMER_A, true);

	TimerEnable(TIMER0_BASE, TIMER_A);


}
//*****************************************************************************
//
//! \addtogroup ADC
//!
//! Initializes the ADC1 using process trigger, sequence 1, sampling
//!	from the temperature sensor
//! \param int channel from 0-7 and 0x80 for Temperature sensor
//
//*****************************************************************************

unsigned long ADC_In(int channel) {
	unsigned long ulADC1_Value[1];
	int i;	
	// The ADC0 peripheral must be enabled for use.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    // Enable sample sequence 3 with a timer signal trigger.  Sequence 3
    // will do a single sample when the timer interrupts sends a singal to start the
    // conversion. 
    ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);

    // Configure step 0 on sequence 3.  Sample the temperature sensor
    // (ADC_CTL_TS) and configure the interrupt flag (ADC_CTL_IE) to be set
    // when the sample is done.  .
    ADCSequenceStepConfigure(ADC0_BASE, 1, 0, (channel |
                             ADC_CTL_END));

    // Since sample sequence 3 is now configured, it must be enabled.
    ADCSequenceEnable(ADC0_BASE, 1);

	//
    // Trigger the ADC conversion.
    //
    ADCProcessorTrigger(ADC0_BASE, 1);

    //
    // Wait for conversion to be completed.
    //
    for (i = 0; i< 350; i++);

    //
    // Read ADC Value.
    //
    ADCSequenceDataGet(ADC0_BASE, 1, ulADC1_Value);
	return ulADC1_Value[0];


}
